package com.glsc.ngateway.platform.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.glsc.ngateway.common.api.platform.dto.user.PlatformUserDto;
import com.glsc.ngateway.common.api.platform.resp.PlatformResponse;
import com.glsc.ngateway.common.base.domain.mysql.gateway.ldap.DepartmentVisitSystem;
import com.glsc.ngateway.platform.aspect.UserCache;
import com.glsc.ngateway.platform.service.LdapDepartmentVisitService;
import com.glsc.ngateway.platform.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;


@RestController
@RequestMapping("/ldapDepartmentVisit")
@Api(value = "信息系统项目人员", tags = {"portal部门权限配置"})
public class LdapDepartmentVisitController {

    private static final Logger logger = LoggerFactory.getLogger(LdapDepartmentVisitController.class);
    @Autowired
    private LdapDepartmentVisitService ldapDepartmentVisitService;
    @Autowired
    private HttpServletRequest request;
    @Autowired
    private UserCache userCache;
    @Autowired
    private UserService userService;

    /*
     * 新增或修改
     */
    @RequestMapping(method = RequestMethod.POST, value = "addOrUpdate")
    @ApiOperation(value = "新增或修改", notes = "新增或修改")
    public PlatformResponse<Void> addOrUpdate(@RequestBody DepartmentVisitSystem departmentVisitSystem, HttpServletRequest request) {

        PlatformUserDto platformUserDto = userCache.get(request);
        if (Objects.nonNull(platformUserDto)) {
            departmentVisitSystem.setUpdateName(platformUserDto.getName());
            departmentVisitSystem.setUpdateAccount(platformUserDto.getUsername());
        }
//        systemMember.setStatus("1");

        ldapDepartmentVisitService.addOrUpdate(departmentVisitSystem);
        return PlatformResponse.successMsg("创建或修改成功");
    }

    /**
     * 没有删除接口，只有下线
     */
    @RequestMapping(method = RequestMethod.POST, value = "delete")
    @ApiOperation(value = "删除", notes = "删除")
    public PlatformResponse<Void> delete(@RequestBody String param) throws Exception {
        if (StringUtils.isEmpty(param)) {
            return PlatformResponse.failedMsg("delete接口，参数为空。");
        }
        JSONObject jsonObject = (JSONObject) JSON.parse(param);
        Integer id = jsonObject.getInteger("id");
        if (id != null) {
            ldapDepartmentVisitService.delete(id);
        }
        return PlatformResponse.successMsg("删除成功");
    }

//    /**
//     * 查询
//     *
//     * @param params
//     * @return
//     */
//    @RequestMapping(method = RequestMethod.POST, value = "search")
//    @ApiOperation(value = "查询", notes = "查询")
//    public PlatformResponse<List<DepartmentVisitSystem>> search(@RequestBody DepartmentVisitSystem params) {
////        if (params.getPageNo() == null) {
////            params.setPageNo(PageConstant.PageNo);
////        }
////        if (params.getPageSize() == null) {
////            params.setPageSize(PageConstant.PageSize);
////        }
//
////        PlatformUserDto user = userCache.get(request);
////        String account = user.getUsername();
////        Set<String> roleNames = user.getRoles().stream().map(role -> role.getName()).collect(Collectors.toSet());
////        if (roleNames.contains("systemAdmin")) {
////            account = null;
////        }
////        params.setMemberAccount(account);
//
//        List<DepartmentVisitSystem> result = ldapDepartmentVisitService.search(params);
//        return PlatformResponse.successData(result);
//    }


    /**
     * 分页查询
     *
     * @param dto
     * @param request
     * @return
     */
    @PostMapping("/page")
    public PlatformResponse<Page<DepartmentVisitSystem>> page(@RequestBody DepartmentVisitSystem dto, HttpServletRequest request) {

        Map<String, Object> param = new HashMap<>();
        if (StringUtils.isNotBlank(dto.getComptitle())) {
            param.put("LIKE_comptitle", dto.getComptitle());
        }
        if (StringUtils.isNotBlank(dto.getDepartment())) {
            param.put("LIKE_department", dto.getDepartment());
        }
        if (StringUtils.isNotBlank(dto.getSystemId())) {
            param.put("LIKE_systemId", dto.getSystemId());
        }
//        param.put("EQ_opId", dto.getSystemId());
        Page<DepartmentVisitSystem> page = ldapDepartmentVisitService.page(dto.getPageNo(), dto.getPageSize(), param);

        return PlatformResponse.successData(page);
    }
}